package com.scs.application.modules.wm.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.modules.wm.entity.Stock;
import com.scs.application.modules.wm.entity.StockProcessLog;
import com.scs.application.modules.wm.mapper.StockProcessLogMapper;
import com.scs.application.modules.wm.service.StockProcessLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Service
public class StockProcessLogServiceImpl extends BaseServiceImpl<StockProcessLogMapper, StockProcessLog> implements StockProcessLogService {

    /**
     * 保存加工历史记录
     * @param stockList
     * @param sourceStockId
     */
    @Override
    public void saveProcessLog(List<Stock> stockList, String sourceStockId) {
        if(stockList == null || stockList.size() == 0)
            return;
        List<StockProcessLog> stockProcessLogList = stockList.stream().map(stock -> {
            StockProcessLog processLog = new StockProcessLog();
            BeanUtils.copyProperties(stock, processLog);
            processLog.setSourceStockId(sourceStockId);
            return processLog;
        }).collect(Collectors.toList());

        super.saveBatch(stockProcessLogList);
    }



    @Override
    public List<StockProcessLog> getStockByLineId(String lineId){
        if(lineId == null){
            throw new BusinessException("未通过审核");
        }
        List<StockProcessLog> stockList = list(Wrappers.<StockProcessLog>query().eq("line_id", lineId));
        return stockList;
    }
}
